package com.itheima.imclient100.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessageBody;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;
import com.itheima.imclient100.R;

import java.util.Date;
import java.util.List;

/**
 * Created by fullcircle on 2017/7/4.
 */

public class ConverstaionAdapter extends RecyclerView.Adapter<ConverstaionAdapter.MyViewHolder> {
    private List<EMConversation> conversationList;

    public ConverstaionAdapter(List<EMConversation> conversationList) {
        this.conversationList = conversationList;
    }

    public void setConversationList(List<EMConversation> conversationList) {
        this.conversationList = conversationList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.list_item_conversation, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        EMConversation emConversation = conversationList.get(position);
        final EMMessage lastMessage = emConversation.getLastMessage();
        EMTextMessageBody body = (EMTextMessageBody) lastMessage.getBody();
        holder.tv_content.setText(body.getMessage());
        // 下面是一个环信自带的时间的工具类。
        holder.tv_time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
        // 下面这个getfrom方法是为了获取当前发送消息的人是谁，但是这样的获取一般不在这里使用，而是在聊天内容中使用
        // getto  是你和谁在聊天
        holder.tv_username.setText(lastMessage.getTo());
        //获取未读消息数量
        int unreadMsgCount = emConversation.getUnreadMsgCount();
        if(unreadMsgCount==0){
            holder.tv_unread.setVisibility(View.GONE);
        }else if(unreadMsgCount>99){
            holder.tv_unread.setVisibility(View.VISIBLE);
            holder.tv_unread.setText("99");

        }else{
            holder.tv_unread.setVisibility(View.VISIBLE);
            holder.tv_unread.setText(""+unreadMsgCount);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onClick(lastMessage.getTo());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return conversationList == null ? 0:conversationList.size();
    }

    static class MyViewHolder extends  RecyclerView.ViewHolder{
        TextView tv_time;
        TextView tv_username;
        TextView tv_unread;
        TextView tv_content;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_username = (TextView) itemView.findViewById(R.id.tv_username);
            tv_unread = (TextView) itemView.findViewById(R.id.tv_unread);
            tv_content = (TextView) itemView.findViewById(R.id.tv_message);

        }
    }

    private MyOnclickListener listener;

    public void setListener(MyOnclickListener listener) {
        this.listener = listener;
    }

    public interface MyOnclickListener{
        void onClick(String username);
    }
}
